package com.awspaas.user.apps.processforsap.impl.send;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.processforsap.model.MessageHeader;
import com.awspaas.user.apps.processforsap.util.HttpClientUtil;
import com.awspaas.user.apps.processforsap.util.ParentInterface;

public class SenderFI006Impl extends ParentInterface {

    public SenderFI006Impl(JSONObject jsonObject) {
        super(jsonObject);
    }

    @Override
    public void dueInterFaceData() {
        String result = "";
        JSONObject totalSendJSON = new JSONObject();
        JSONObject totalResultJSON = new JSONObject();
        JSONObject Responses = new JSONObject();
        Responses.put("Type","S");
        totalResultJSON.put("Responses",Responses);
        try {
            MessageHeader messageHeader = super.messageHeader;
            JSONObject sendData = super.sendData;
            JSONArray jsonArray = sendData.getJSONArray("Requests");
            BO configBo = messageHeader.getConfigBo();
            String url = configBo.getString("SEND_URL");
            for (int i =0;i<jsonArray.size();i++){
                JSONObject jsonObjectSend= new JSONObject();
                JSONObject  messageHeader111=sendData.getJSONObject("MessageHeader");
                jsonObjectSend.put("MessageHeader",messageHeader111);
                JSONObject data = jsonArray.getJSONObject(i);
                String id = data.getString("ID");
                String resultData = data.getString("RESULT");
                if ("S".equals(resultData)){
                    continue;
                }
                jsonObjectSend.put("Requests",data);
                totalSendJSON.put(i+"",jsonObjectSend);
                result = HttpClientUtil.doPost(url, jsonObjectSend.toJSONString(), SDK.getRuleAPI().executeAtScript("@getSapParamas(PONAME)"), SDK.getRuleAPI().executeAtScript("@getSapParamas(POPASSWORD)"));
                totalResultJSON.put(i+"",result);

                JSONObject jsonObject = JSONObject.parseObject(result);
                JSONObject responses = jsonObject.getJSONObject("Responses");
                //封装最终数据
                String msgType = responses.getString("MSGTYP");
                String text = responses.getString("MSGTXT");
                String belnr = responses.getString("BELNR");

                if ("S".equals(msgType)){
                    BO bo = SDK.getBOAPI().query("BO_EU_SAP_ZCJSLIST").detailById(id);
                    bo.set("RESULT","S");
                    bo.set("SAP_RESULT",belnr);
                    SDK.getBOAPI().update("BO_EU_SAP_ZCJSLIST",bo);
                    Responses.put("Type","S");
                    totalResultJSON.put("Responses",Responses);
                }else{
                    Responses.put("Type","E");
                    totalResultJSON.put("Responses",Responses);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            super.resultType = false;
            throw new BPMNError("0331",e.getMessage());
        } finally {
            super.resultSendData = totalSendJSON.toJSONString();
            super.returnData = JSONObject.toJSONString(totalResultJSON);
        }

    }

}